﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YBApproval.Model;
using YBApproval.IDAL;
using YBApproval.BLL;

namespace YBApproval
{
    public class PageDataBandHelper
    {

        public static List<InstantView> GetInstantView(List<InstanceApproval> Items)
        {
            IList<Form_ProcessSet> Processes;
            using (IProcess IProcessDAL = DALFactory.GetDAL<IProcess>())
            {
                Processes = IProcessDAL.GetAllProcesses();
            }

            List<InstantView> NewView = new List<InstantView>();

            using (INode INodeDAL = DALFactory.GetDAL<INode>())
            {
                NewView = new List<InstantView>();
                foreach (var item in Items)
                {
                    InstantView ViewItem = new InstantView
                    {
                        CloseDate = item.Record.CloseDate,
                        ClosedBy = item.Record.ClosedBy,
                        CurrentNodeId = item.Record.CurrentNodeId,
                        FormId = item.Record.FormId,
                        IsClosed = item.Record.IsClosed,
                        IsSubmitted = item.Record.IsSubmitted,
                        ProcessId = item.Record.ProcessId,
                        RecordId = item.Record.RecordId,
                        Step = item.Step,
                        SubmitDate = item.Record.SubmitDate,
                        Submitter = item.Record.Submitter,
                        SubmitterId = item.Record.SubmitterId,
                        UpdateDate = item.Record.UpdateDate
                    };

                    var pro = Processes.SingleOrDefault(p => p.ProcessId == item.Record.ProcessId);
                    if (pro != null)
                        ViewItem.ProcessName = pro.Name;

                    var node = INodeDAL.GetNode(item.NodeId);
                    if (node != null)
                        ViewItem.StepName = node.Name;

                    NewView.Add(ViewItem);
                }
            }
            return NewView;
        }



        internal static List<InstantView> GetInstantView(List<Instance> Drafts)
        {
            IList<Form_ProcessSet> Processes;
            using (IProcess IProcessDAL = DALFactory.GetDAL<IProcess>())
            {
                Processes = IProcessDAL.GetAllProcesses();
            }

            List<InstantView> NewView = new List<InstantView>();

            using (INode INodeDAL = DALFactory.GetDAL<INode>())
            {
                NewView = new List<InstantView>();
                foreach (var item in Drafts)
                {
                    InstantView ViewItem = new InstantView
                    {
                        CloseDate = item.CloseDate,
                        ClosedBy = item.ClosedBy,
                        CurrentNodeId = item.CurrentNodeId,
                        FormId = item.FormId,
                        IsClosed = item.IsClosed,
                        IsSubmitted = item.IsSubmitted,
                        ProcessId = item.ProcessId,
                        RecordId = item.RecordId,
                        Step = item.Step,
                        SubmitDate = item.SubmitDate,
                        Submitter = item.Submitter,
                        SubmitterId = item.SubmitterId,
                        UpdateDate = item.UpdateDate
                    };

                    var pro = Processes.SingleOrDefault(p => p.ProcessId == item.ProcessId);
                    if (pro != null)
                        ViewItem.ProcessName = pro.Name;

                    var node = INodeDAL.GetNode(item.CurrentNodeId);
                    if (node != null)
                        ViewItem.StepName = node.Name;

                    NewView.Add(ViewItem);
                }
            }
            return NewView;
        }
    }
}